package com.miplus.ccrm.core.service.strategy;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.segments.MergeSegments;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.github.yulichang.base.MPJBaseServiceImpl;
import com.github.yulichang.wrapper.MPJLambdaWrapper;
import com.miplus.ccrm.common.CmdResVo;
import com.miplus.ccrm.common.EnumRCode;
import com.miplus.ccrm.common.JPubUtils;
import com.miplus.ccrm.common.ServiceException;
import com.miplus.ccrm.core.controller.vo.strategy.*;
import com.miplus.ccrm.core.pojo.CfgAssignratio;
import com.miplus.ccrm.core.pojo.CfgAssignset;
import com.miplus.ccrm.core.mapper.CfgAssignsetMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
* @author pc
* @description 针对表【cfg_assignset】的数据库操作Service实现
* @createDate 2024-04-16 15:12:00
*/
@Service
public class CfgAssignsetServiceImpl extends MPJBaseServiceImpl<CfgAssignsetMapper, CfgAssignset>
    implements CfgAssignsetService{

    @Resource
    private CfgAssignratioService cfgAssignratioService;

    @Override
    public IPage<AssSetResVo> assignSetList(String $usrid, AssSetReqVo paras)
    {
        return selectJoinListPage(new Page<>(paras.getPageNum(), paras.getPageSize()),
                AssSetResVo.class,
                new MPJLambdaWrapper<CfgAssignset>()
                        .selectAs("(select g.grpname from sys_grp g where g.grpid=t.grpid)", AssSetResVo::getGrpname)
                        .selectAs("fun_getnames('ACTIVITY.QUERY.DISMODE',t.modeval)",AssSetResVo::getModevaldsc)
                        .selectAs("fun_getnames('PUBLIC.ENSTATUS',t.status)",AssSetResVo::getStatusdsc)
                        .selectAll(CfgAssignset.class)
                        .eq(JPubUtils.IsNotEmpty(paras.getGrpid()),CfgAssignset::getGrpid,paras.getGrpid())
                        .eq(JPubUtils.IsNotEmpty(paras.getStatus()),CfgAssignset::getStatus,paras.getStatus())
                        .orderByAsc(CfgAssignset::getGrpid)
        );
    }

    @Override
    public CmdResVo assignSetUpdate(String $usrid, AssSetEditVo paras)
    {
        CfgAssignset def=new CfgAssignset();
        BeanUtils.copyProperties(paras,def);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.update(def,
                new MPJLambdaWrapper<CfgAssignset>().eq(CfgAssignset::getGrpid,paras.getGrpid())
        );
        cfgAssignratioService.getBaseMapper().delete(
                new QueryWrapper<CfgAssignratio>().eq("grpid",paras.getGrpid())
        );
        AddAssignUsr(paras);
        return new CmdResVo(ret,def.getGrpid());
    }

    @Override
    public CmdResVo assignSetInsert(String $usrid,AssSetEditVo paras)
    {
        CfgAssignset def=baseMapper.selectById(paras.getGrpid());
        if(def!=null){
            throw new ServiceException(EnumRCode.HasExist.value, EnumRCode.HasExist.name);
        }
        def=new CfgAssignset();
        BeanUtils.copyProperties(paras,def);
        def.setStartrun(1);
        def.setMddt(new Date());
        def.setMdusr($usrid);
        int ret=baseMapper.insert(def);

        AddAssignUsr(paras);

        return new CmdResVo(ret,def.getGrpid());
    }

    private void AddAssignUsr(AssSetEditVo paras)
    {
        if(JPubUtils.IsEmpty(paras.getAssUsrList()))
            return;

        for(AssRatioAddVo addVo : paras.getAssUsrList()){
            CfgAssignratio cfgAssignratio=new CfgAssignratio();
            cfgAssignratio.setGrpid(paras.getGrpid());
            cfgAssignratio.setUsrid(addVo.getUsrid());
            cfgAssignratio.setRatio(addVo.getRatio());

            cfgAssignratioService.getBaseMapper().insert(cfgAssignratio);
        }
    }
    @Override
    public CmdResVo assignSetDelete(String grpid)
    {
        int ret=baseMapper.delete(new QueryWrapper<CfgAssignset>()
                        .eq("grpid", grpid)
        );
        cfgAssignratioService.getBaseMapper().delete(
                new QueryWrapper<CfgAssignratio>().eq("grpid",grpid)
        );
        return new CmdResVo(ret,grpid);
    }

    @Override
    public List<AssRatioResVo> assignRatioList(String grpid)
    {
        return baseMapper.assignRatioList(grpid);
    }
}




